#include "server.h"
#include "svr_util/include/read_cfg.h"
#include "inner_con.h"

using namespace std;
using namespace proto;
using namespace lc;

bool Server::Init()
{
	L_COND_F(m_cfg.Init());
	bool r = false;
	r = RpcNetSvr<InnerSvrCon>::Ins().Init(m_cfg.GetInnerPort(), m_cfg.GetInnerIp());
	L_COND_F(r);
	AccRpcSvr::Init();

	r = m_clientListener.Init(m_cfg.GetExPort(), m_cfg.GetExIp());
	L_COND_F(r);

	L_DEBUG("Server::Init ok");
	return true;
}

ExternalSvrCon * Server::FindExternalSvrCon(uint64 cid)
{
	SvrCon *pClient = m_clientListener.GetConnMgr().FindConn(cid);
	ExternalSvrCon *p = dynamic_cast<ExternalSvrCon *>(pClient);
	if (nullptr == p)
	{
		L_DEBUG("find ClientSvrCon fail. cid=%lld", cid);
	}
	return p;
}

uint32 Server::GetExConSize() const
{
	return m_clientListener.GetConstConnMgr().GetConSize();
}

bool CfgMgr::Init()
{
	L_DEBUG("init cfg");
	su::Config cfg;
	L_COND_F(cfg.init("cfg.txt"));

	m_inner_ip = cfg.GetStr("inner_ip");
	L_DEBUG("inner_ip=%s", m_inner_ip.c_str());
	m_inner_port = (uint16)cfg.GetInt("inner_port");
	L_DEBUG("inner_port=%d", m_inner_port);

	m_ex_ip = cfg.GetStr("ex_ip");
	L_DEBUG("ex_ip=%s", m_ex_ip.c_str());
	m_ex_port = (uint16)cfg.GetInt("ex_port");
	L_DEBUG("ex_port=%d", m_ex_port);

	max_send_buf_size = (bool)cfg.GetInt("max_send_buf_size");
	L_DEBUG("max_send_buf_size=%d", max_send_buf_size);
	return true;
}
